<?php

namespace App\Model\Admin;

use App\Model\BaseModel;
use App\Service\Admin\UserRoleService;

/**
 * 后台用户-模型
 * @author LZH
 * @since: 2021/08/12
 * Class AdminModel
 */
class AdminModel extends BaseModel
{
    // 设置数据表名
    protected $tableName = DB_PREFIX . "admin";

    public function getList(array $where = [], $sort = [], string $limit = '', $append = [], $visible = [], $hidden = [])
    {
        $this->log_info('获取管理员列表');
        $append = ['roles','level_name','position_name','gender_name'];
        return parent::getList($where, $sort, $limit, $append, $visible, $hidden);
    }

    /**
     * 获取缓存信息
     * @param int $id 记录ID
     */
    public function getInfoById($id, $append = [], $visible=[], $hidden = [])
    {
        $append = ['roles','level_name','position_name','gender_name'];
        return parent::getInfoById($id, $append);
//        if ($info) {
//            // 头像
//            if ($info['avatar']) {
//                $info['avatar'] = get_image_url($info['avatar']);
//            }
//
//            // 性别
//            if ($info['gender']) {
//                $info['gender_name'] = $this->gender_list[$info['gender']];
//            }
//
//            // 职级
//            if ($info['level_id']) {
//                $levelMod = new LevelModel();
//                $levelInfo = $levelMod->getInfoById($info['level_id']);
//                $info['level_name'] = $levelInfo['name'];
//            }
//
//            // 岗位
//            if ($info['position_id']) {
//                $positionModel = new PositionModel();
//                $positionInfo = $positionModel->getInfoById($info['position_id']);
//                $info['position_name'] = $positionInfo['name'];
//            }
//
//            // 获取用户角色列表
//            $userRoleModel = new UserRoleModel();
//            $roleList = $userRoleModel->getUserRoleList($id);
//            $info['roles'] = $roleList;
//        }
//        return $info;
    }

    protected function getRolesAttr($value, $data)
    {
        $this->log_info('获取管理员的角色信息');
        $userRoleModel = new UserRoleModel();
        $roleList = $userRoleModel->getUserRoleList($data['id']);
        return $roleList;
    }

    protected function getLevelNameAttr($value, $data)
    {
        $this->log_info('获取管理员的职级信息');
        $levelModel = new LevelModel();
        return $levelModel->where($levelModel->schemaInfo()->getPkFiledName(), $data['level_id'])->val('name');
    }

    protected function getPositionNameAttr($value, $data)
    {
        $this->log_info('获取管理员的职级信息');
        $positionModel = new PositionModel();
        return $positionModel->where($positionModel->schemaInfo()->getPkFiledName(), $data['position_id'])->val('name');
    }

    // 头像
    protected function getAvatarAttr($value, $data)
    {
        return get_image_url($value);
    }

    // 性别
    protected function getGenderNameAttr($value, $data)
    {
        return $this->gender_list[$data['gender']];
    }
}
